//
//  STMRefundAuditView.m
//  StoreManagement
//
//  Created by centling on 16/6/3.
//  Copyright © 2016年 alfred. All rights reserved.
//

#import "STMRefundAuditView.h"


@implementation STMRefundAuditView


+ (instancetype)refundAuditView {
    return [[[NSBundle mainBundle] loadNibNamed:@"STMRefundAuditView" owner:self options:nil] lastObject];
}

- (void)awakeFromNib {
    self.auditContentTextView.delegate = self;
    self.auditContentTextView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.auditContentTextView.layer.borderWidth = 1;
    self.auditContentTextView.layer.cornerRadius = 5;
    
    self.firstButton = [[DLRadioButton alloc] initWithFrame:CGRectMake(0, 45, 90, 25)];
    self.secondButton = [[DLRadioButton alloc] initWithFrame:CGRectMake(130, 45, 90, 25)];
    [self addSubview:self.firstButton];
    [self addSubview:self.secondButton];
    self.firstButton.iconColor = [UIColor blueColor];
    self.firstButton.indicatorColor = [UIColor blueColor];
    self.secondButton.iconColor = [UIColor blueColor];
    self.secondButton.indicatorColor = [UIColor blueColor];
    [self.firstButton setTitle:@"弃货" forState:UIControlStateNormal];
    [self.secondButton setTitle:@"需要货物" forState:UIControlStateNormal];
    self.firstButton.titleLabel.font = [UIFont systemFontOfSize:14];
    self.secondButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.firstButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.secondButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    NSMutableArray *mutArray = [NSMutableArray arrayWithObject:self.secondButton];
    self.firstButton.otherButtons = mutArray;
    self.firstButton.selected = YES;
    self.firstButton.hidden = YES;
    self.secondButton.hidden = YES;
}


- (void)adjustTextViewConstraint {
    self.firstButton.hidden = NO;
    self.secondButton.hidden = NO;
    self.textViewTopConstraint.constant = 58;
    self.placeHolderTopConstraint.constant = 65;
}

- (void)adjustForConsultReply {
    self.titleLabel.text = @"回复";
    [self.agreeButton setTitle:@"确定" forState:UIControlStateNormal];
    [self.disagreeButton setTitle:@"取消" forState:UIControlStateNormal];
}

- (void)adjustForOrderCancel {
    self.placeholderLabel.text = @"请输入取消订单原因";
    self.titleLabel.text = @"取消订单";
    [self.agreeButton setTitle:@"确定" forState:UIControlStateNormal];
    [self.disagreeButton setTitle:@"取消" forState:UIControlStateNormal];
}


- (IBAction)agreeAction:(id)sender {
    if ([self.auditContentTextView.text isEqualToString:@""]) {
        if (self.forOrderCancel) {
            [GlobalUtil HUDShowMessage:@"请输入取消原因" addedToView:self];
        } else {
            [GlobalUtil HUDShowMessage:@"请输入回复内容" addedToView:self];
        }
        return;
    }
    if (self.firstButton.selected) {
        if ([self.delegate respondsToSelector:@selector(agreeOrDisagreeWithState:Message:returnType:)]) {
            [self.delegate agreeOrDisagreeWithState:@"2" Message:self.auditContentTextView.text returnType:@"1"];
        }
    } else {
        if ([self.delegate respondsToSelector:@selector(agreeOrDisagreeWithState:Message:returnType:)]) {
            [self.delegate agreeOrDisagreeWithState:@"2" Message:self.auditContentTextView.text returnType:@"2"];
        }
    }
    
}


- (IBAction)disagreeAction:(id)sender {
    if (!self.forConsultReply && !self.forOrderCancel) {
        if ([self.auditContentTextView.text isEqualToString:@""]) {
            [GlobalUtil HUDShowMessage:@"请输入回复内容" addedToView:self];
            return;
        }
    }
    if (self.firstButton.selected) {
        if ([self.delegate respondsToSelector:@selector(agreeOrDisagreeWithState:Message:returnType:)]) {
            [self.delegate agreeOrDisagreeWithState:@"3" Message:self.auditContentTextView.text returnType:@"1"];
        }
    } else {
        if ([self.delegate respondsToSelector:@selector(agreeOrDisagreeWithState:Message:returnType:)]) {
            [self.delegate agreeOrDisagreeWithState:@"3" Message:self.auditContentTextView.text returnType:@"2"];
        }
    }
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if (![text isEqualToString:@""]) {
        _placeholderLabel.hidden = YES;
    }
    if ([text isEqualToString:@""] && range.location == 0 && range.length == 1) {
        _placeholderLabel.hidden = NO;
    }
    return YES;
}

@end
